草庐IT

C++ std::move 混淆

全部标签

c++ - std::string 和 std::wstring 的前向声明

不能转发声明std::string和std::wstring的问题经常被讨论。据我了解,原因是这些类型是模板类basic_string实例化的类型定义:namespacestd{typedefbasic_stringstring;typedefbasic_stringwstring;}并且该语言不允许typedef的前向声明。使用继承而不是typedef对于c++标准来说不是更好吗:namespacestd{classstring:publicbasic_string{};classwstring:publicbasic_string{};}这样我们就可以转发声明std::string

c++ - 如何创建 std::tuple 的别名?

我正在学习C++,我想知道如何为std::tuple创建别名。我想做你可能用std::tuple做的事情,但使用另一个名字。可能吗?谢谢。 最佳答案 templateusingmy_tuple=std::tuple;Livedemolink. 关于c++-如何创建std::tuple的别名?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/25545334/

c++ - 使用 std::enable_if 和可变参数基类

这是我正在尝试做的一个简化示例:#include#include#includetemplateclassfoo{public:templatetypenamestd::enable_if::value>::typebar(constU&t){std::coutclassbaz:publicfoo...{};intmain(){bazb;b.bar(1.0);}这给了我模棱两可的函数错误:error:requestformember'bar'isambiguousb.bar(1.0);note:candidatesare:templatetypenamestd::enable_if::

c++ - 使用 std::enable_if 有什么问题?

我有一个函数set_data,我必须针对它接收的不同类型以不同的方式实现它。例如,它试图根据输入类型实现两个重载。如果是基本的,非void和非nullptr_t,则在第一次实现时处理它。如果它是std::string或char缓冲区,则以第二种方式处理它。structfield{templatevoidset_data(TDataTypedata,size_tindex=0);};template::value&&std::is_same::value==false&&std::is_same::value==false>::type>voidfield::set_data(TData

c++ - 涉及 const unique_ptr 的 move 构造函数

在下面的代码中,我将p设置为const,因为它在Foo的生命周期内永远不会指向任何其他int。这不会编译,因为调用了unique_ptr的复制构造函数,这显然已被删除。除了使p非常量之外还有什么解决方案吗?谢谢。#includeusingnamespacestd;classFoo{public://xisalargestructinrealityFoo(constint*constx):p(x){};Foo(Foo&&foo):p(std::move(foo.p)){};private:constunique_ptrp;}; 最佳答案

c++ - 无法直接分配元素 std::array,它说没有运算符 "="匹配

在我的processor类有一个数组.voidProcessor::addJob(Jobjob){this->Jobs[this->uBound]=job;this->uBound++;}我想在我的processor中添加一个新元素类(class)。为此,我创建了新的private:intuBound=0我的属性(property)processor类(class)。我直接分配this->Jobs[this->uBound]=job;.它给了我一个错误。它说Error:nooperator"="matchestheseoperands.operandtypesare:std::arra

c++ - 如何强制调用 move 构造函数(C++)?

我有这对代码行:#includeusingnamespacestd;classA{public:A()noexcept{cout他们产生这个输出:A::A()A::A(constA&)A::A()A::A(constA&)我需要做什么才能从B::B(A&&)调用A::A(A&&)?如您所见,添加noexcept并不能解决这个问题。 最佳答案 尽管a的类型是对A的右值引用,但a本身是一个左值。要保留其右值性,您需要使用std::move:B(A&&a)noexcept:_a(std::move(a)){}

c++ - 有没有更优雅的方式在 C++ 中结合 sprintf 和 std::string?

在我的C++代码中,我经常使用以下类型的辅助函数:staticinlinestd::stringstringf(constchar*fmt,...){std::stringret;//Dealwithvarargsva_listargs;va_start(args,fmt);//Resizeourstringbasedontheargumentsret.resize(vsnprintf(0,0,fmt,args));//Endthevarargsandrestartbecausevsnprintfmuckedupourargsva_end(args);va_start(args,fmt

c++ - 使用 std::function 参数重载函数:为什么从未调用 const 方法?

#include#include#include#includeusingnamespacestd;classA{public:voiddoStuff(functionfunc)const{coutfunc){cout(str);func(mutableString);});}private:vectorm_vec;};intmain(){autoa=A{};a.doStuff([](string*str){*str="Imodifiedthisstring";});}在此示例中,从未调用const方法。如果代码看起来很奇怪,这就是我正在尝试做的事情:我让客户通过传递一个函数来迭代对象

c++ - 将 boost::log::expressions::attr< std::string > 转换为 std::string

在使用Boost.Log时,我试图保留我的TimeStamp格式化程序,例如:logging::add_file_log(keywords::file_name="my.log",keywords::format=(expr::stream("TimeStamp","%Y-%m-%d%H:%M:%S")("Line")("File")据说我不能使用其他形式的格式化程序,因为我将很难转换"TimeStamp"转换成我的自定义格式:staticvoidmy_formatter(logging::record_viewconst&rec,logging::formatting_ostream